<?php
/**
 * @version		0.2.0 joomla zmluvy $
 * @package		com_zjmluvy
 * @copyright	Copyright © 2011 - All rights reserved.
 * @license		GNU/GPL
 * @author    AdrianKo		
 * @website   http://code.google.com/p/joomla-zmluvy
 *
 *
 *1.3.2012
 */

// no direct access
defined('_JEXEC') or die('Restricted access');

jimport('joomla.application.component.controller');

class jzmluvyControllerUpravakategorie extends JController
{

	function __construct()
	{
		parent::__construct();
		
		// Registrace extra dotazu
		$this->registerTask( 'add'  , 	'display'  );
		$this->registerTask( 'edit'  , 	'display'  );
		$this->registerTask( 'cancel'  , 	'display'  );
		$this->registerTask( 'apply', 	'save'  );
		$this->registerTask( 'remove', 	'delete'  );
		$this->registerTask( 'publish', 	'publish'  );
	}

  	/**
	 * Displays a view
	 */
	function display( )
	{
		switch($this->getTask())
		{
			case 'add'     :
			{	JRequest::setVar( 'hidemainmenu', 1 );
//				JRequest::setVar( 'layout', 'form'  );
				JRequest::setVar( 'view', 'upravakategorie' );
				JRequest::setVar( 'edit', false );
				
			} break;
			case 'edit'    :
			{
				JRequest::setVar( 'hidemainmenu', 1 );
//				JRequest::setVar( 'layout', 'form'  );
				JRequest::setVar( 'view', 'upravakategorie' );
				JRequest::setVar( 'edit', true );
			} break;
			
			case 'cancel'    :
			{
				JRequest::setVar( 'view', 'kategorie' );
			} break;
		}

		parent::display();
	}
	
		function save()
	{
		JRequest::checkToken() or die( 'Invalid Token' );
		
		$task		= JRequest::getVar('task');

		$post = JRequest::get( 'post' );
		$post['description'] = JRequest::getVar( 'description', '', 'post','string', JREQUEST_ALLOWRAW );
		$post['description']	= str_replace( '<br>', '<br />', $post['description'] );

		$model = $this->getModel('upravakategorie');
		
		$returnid = $model->store($post);

		if ($returnid) {

			switch ($task)
			{
				case 'apply' :
					$link = 'index.php?option=com_jzmluvy&view=upravakategorie&hidemainmenu=1&cid[]='.$returnid;
					break;

				default :
					$link = 'index.php?option=com_jzmluvy&view=kategorie';
					break;
			}
			$msg	= 'Kategorie přidána či upravena';

		} else {

			$msg 	= '';
			$link = 'index.php?option=com_jzmluvy&view=kategorie';

		}

//		$model->checkin();

		$this->setRedirect( $link, $msg );
 	}
 	
  function delete() {
    JRequest::checkToken() or die( 'Invalid Token' );
    $db 		=& JFactory::getDBO();
    $cid = JRequest::getVar( 'cid', '', '', 'array' );
    
    $where = ' WHERE catid IN('.implode($cid,',').')';
    $where_kat = ' WHERE id IN('.implode($cid,',').')';
    
    $query = 'SELECT COUNT(id)'
			. ' FROM #__jzmluvy'
			. $where
		;
		$db->setQuery( $query );
		$total = $db->loadResult();
		
    if ($total != 0) { 
      $msg 	= 'Není možné smazat kategorii, která obsahuje záznam!';
      $link = 'index.php?option=com_jzmluvy&view=kategorie';
      $this->setRedirect( $link, $msg, false );
      return false;  
    }        
    
    $query = 'DELETE FROM #__jzmluvy_kategorie'. $where_kat;
    
    $db->setQuery($query);
    $result = $db->query();

    $msg 	= 'Kategorie byly vymazány';
		$link = 'index.php?option=com_jzmluvy&view=kategorie';
			
		$this->setRedirect( $link, $msg );   
  }

  function publish()
  {
    global $mainframe;

    // Check for request forgeries
    JRequest::checkToken() or jexit( 'Invalid Token' );
	
    $cid = JRequest::getVar( 'cid', '', '', 'array' );
    $publish = 1;

    // Initialize variables
    $db		=& JFactory::getDBO();
    $user	=& JFactory::getUser();
    $uid	= $user->get('id');

    JArrayHelper::toInteger($cid);

    if (count( $cid ) < 1) {
		  $action = $publish ? 'publish' : 'unpublish';
		  JError::raiseError(500, JText::_( 'Select a category to '.$action, true ) );
    }

    $cids = implode( ',', $cid );

    $query = 'UPDATE #__jzmluvy_kategorie'
    . ' SET published = ' . (int) $publish
    . ' WHERE id IN ( '.$cids.' )'
    . ' AND ( checked_out = 0 OR ( checked_out = '.(int) $uid.' ) )'
    ;
    $db->setQuery( $query );
    if (!$db->query()) {
		  JError::raiseError(500, $db->getErrorMsg() );
    }

/*	if (count( $cid ) == 1) {
		$row =& JTable::getInstance('category');
		$row->checkin( $cid[0] );
	} */

    $msg 	= 'Kategorie byly povoleny';
		$link = 'index.php?option=com_jzmluvy&view=kategorie';
			
		$this->setRedirect( $link, $msg );
  }
  
    function unpublish()
  {
    global $mainframe;

    // Check for request forgeries
    JRequest::checkToken() or jexit( 'Invalid Token' );
	
    $cid = JRequest::getVar( 'cid', '', '', 'array' );
    $publish = 0;

    // Initialize variables
    $db		=& JFactory::getDBO();
    $user	=& JFactory::getUser();
    $uid	= $user->get('id');

    JArrayHelper::toInteger($cid);

    if (count( $cid ) < 1) {
		  $action = $publish ? 'publish' : 'unpublish';
		  JError::raiseError(500, JText::_( 'Select a category to '.$action, true ) );
    }

    $cids = implode( ',', $cid );

    $query = 'UPDATE #__jzmluvy_kategorie'
    . ' SET published = ' . (int) $publish
    . ' WHERE id IN ( '.$cids.' )'
    . ' AND ( checked_out = 0 OR ( checked_out = '.(int) $uid.' ) )'
    ;
    $db->setQuery( $query );
    if (!$db->query()) {
		  JError::raiseError(500, $db->getErrorMsg() );
    }

/*	if (count( $cid ) == 1) {
		$row =& JTable::getInstance('category');
		$row->checkin( $cid[0] );
	} */

    $msg 	= 'Kategorie byly zakázány';
		$link = 'index.php?option=com_jzmluvy&view=kategorie';
			
		$this->setRedirect( $link, $msg );
  }
}
?>